<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业" prop="enterpriseId">
        <el-select
          v-model="queryParams.enterpriseId"
          placeholder="请选择企业"
          clearable
          filterable
        >
          <el-option
            v-for="dict in enterpriseDict"
            :key="dict.id"
            :label="dict.enterpriseName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入问题标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否解决" prop="resolved">
        <el-select
          v-model="queryParams.resolved"
          placeholder="请选择是否解决"
          clearable
          filterable
        >
          <el-option
            v-for="dict in resolved"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:question:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:question:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:question:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:question:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="问题标题" align="center" prop="title" />
      <el-table-column label="问题类型" align="center" prop="questionType" />
      <!-- <el-table-column label="问题详情内容" align="center" prop="content" /> -->
      <el-table-column label="是否解决" align="center" prop="questionResolved" />
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, '{y}-{m}-{d} ') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:question:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:question:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-operation"
            @click="handleInfo(scope.row)"
          >流程信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改问题流程信息 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业" prop="enterpriseId">
              <el-select
                v-model="form.enterpriseId"
                placeholder="请选择企业"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in enterpriseDict"
                  :key="dict.id"
                  :label="dict.enterpriseName"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="问题标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入问题标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择问题类型"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in dict.type.question_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否解决" prop="resolved">
              <el-select
                v-model="form.resolved"
                placeholder="请选择是否解决"
                filterable
              >
                <el-option
                  v-for="dict in resolved"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="问题详情内容">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改问题反映对话框 -->
    <el-dialog :title="title" :visible.sync="infoOpen" width="1000px" append-to-body>
      <el-form ref="infoForm" :model="infoForm" :rules="rules" label-width="100px">
        <span style="font-size:16px;margin-left:30px">企业信息&emsp;:</span>
        <el-row style="margin-top:20px;margin-left:50px;">
          <el-col :span="12">
            <div>企业名称：{{enterprise.enterpriseName}}</div>
          </el-col>
          <el-col :span="12">
            <div>联系人：{{enterprise.contactPerson}}</div>
          </el-col>
        </el-row>
        <div style="font-size:16px;margin-left:30px;margin-top:20px;">工单信息&emsp;:</div>
        <el-row style="margin-top:20px;margin-left:50px;">
          <el-col :span="12">
            <div>分类：{{infoForm.questionType}}</div>
          </el-col>
          <el-col :span="12">
            <div>问题标题：{{infoForm.title}}</div>
          </el-col>
        </el-row>
        <el-row style="margin-top:20px;margin-left:50px;">
          <el-col :span="12">
            <div>提出时间：{{infoForm.createTime}}</div>
          </el-col>
        </el-row>
        <el-row style="margin-top:20px;margin-left:50px;">
          <el-col :span="24">
            <span>问题详情内容：</span>
            <span v-html="infoForm.content"></span>
          </el-col>
        </el-row>
        <el-row style="margin-top:20px;margin-left:50px;">
          <el-col :span="24">
            <span>附件：</span>
            <div class="files">
              <div v-for="(item,index) in fileList" :key="index">
                <img :src="item.url" class="fileimg"/>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div style="font-size:16px;margin-left:30px;margin-top:20px;">流程信息&emsp;:</div>
      <div class="block">
        <el-timeline>
          <el-timeline-item :timestamp = "item.createTime" placement="top" v-for="(item,index) in questionSchedules" :key="index">
            <el-card class="blockcard">
              <h4>{{ item.createName }}</h4>
              <input type="text" v-model="item.flowContent" class="blockinput" :disabled = "infoForm.resolved == 1 || !isZy">
              <i class="el-icon-close blockimg" @click="delflow(index)"></i>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="submitresolved" :disabled = "infoForm.resolved == 1 || !isZy">终 止</el-button>
        <el-button type="success" @click="reply" :disabled = "infoForm.resolved == 1 || !isZy">回 复</el-button>
        <el-button type="primary" @click="submitSchedules" :disabled = "infoForm.resolved == 1">保 存</el-button>
        <el-button @click="cancelSchedules">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion } from "@/api/system/question";
import { getEnterpriseDict } from "@/api/system/enterprise";
export default {
  name: "Question",
  dicts:[
    "question_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题反映表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示流程信息弹出层
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        title: null,
        type: null,
        content: null,
        resolved: null,
        createName: null,
        updateName: null,
      },
      // 表单参数
      form: {},
      // 流程信息表单参数
      infoForm:{},
      // 表单校验
      rules: {
      },
      // 企业字典表
      enterpriseDict:[],
      // 问题是否已解决
      resolved:[{name:'未解决',id:0},{name:'已解决',id:1}],
      // 流程控制中的企业信息
      enterprise:{},
      // 问题流程控制
      questionSchedules:[],
      fileList:[],
      // 判断当前用户是否为企业专员
      isZy:false,
    };
  },
  created() {
    this.getList();
    this.getEnterDict();
    // 判断当前用户是否为企业专员
    this.isZy = (this.$auth.hasRole("zy") && !this.$auth.hasRole("admin")) 
  },
  methods: {
    /** 查询问题反映列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取所有企业的id和name
    getEnterDict(){
      getEnterpriseDict().then(response => {
        this.enterpriseDict = response.data
      }).catch(error => {
        console.log(error)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 流程控制取消按钮
    cancelSchedules(){
      this.infoOpen = false;
      this.questionSchedules = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        title: null,
        type: null,
        content: null,
        resolved: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        createName: null,
        updateBy: null,
        updateTime: null,
        updateName: null,
        resolved:0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题反映";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题反映";
      });
    },
    // 流程信息按钮操作
    handleInfo(row){
      this.reset();
      getQuestion(row.id).then(response => {
        this.infoForm = response.data;
        this.fileList = response.data.attachFileList
        this.questionSchedules = response.data.questionSchedules;
        this.infoOpen = true;
        this.title = "问题处理";
        this.enterprise = response.data.enterprise;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 问题流程控制提交按钮
    submitSchedules(){
      let data = {
        id:this.infoForm.id,
        questionSchedules : this.questionSchedules,
        resolved:this.infoForm.resolved
      }
      updateQuestion(data).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.infoOpen = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题反映编号为"' + ids + '"的数据项？').then(function() {
        return delQuestion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    },
    // 回复按钮
    reply(){
      var username = this.$store.state.user.name;
      var stamp= new Date().getTime() + 8 * 60 * 60 * 1000;
      var time = new Date(stamp).toISOString().replace(/T/, ' ').replace(/\..+/, '').substring(0, 19);
      this.questionSchedules.unshift({id:null,questionId:null,flowTitle:null,flowContent:null,createTime:time,createName:username})
    },
    // 点击卡片删除按钮
    delflow(item){
      this.questionSchedules.splice(item,1)
    },
    submitresolved(){
      let data = {
        id:this.infoForm .id,
        resolved:1,
      }
      this.$modal.confirm('是否终止此问题？').then(function() {
        updateQuestion(data).then(response => {
        });
      }).then(() => {
        this.$modal.msgSuccess("终止成功");
        this.infoOpen = false;
        this.getList()
      }).catch(() => {});
    },
  }
};
</script>
<style lang="scss" scoped>
.block{
  margin-top: 30px;
  .blockcard{
    position: relative;
    .blockimg{
      position: absolute;
      top: 10px;
      right: 10px;
    }
  }
}
.files{
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 10px;
  .fileimg{
    padding-left: 10px;
    width: 140px;
  }
}
::v-deep .el-select {
  width: 100%;
}
.blockinput{
  border:1px #dbdbdb solid;
  width: 800px;
  height: 40px;
  padding-left: 10px;
}
.blockinput:focus {
  border: 2px solid #dbdbdb; /* 设置输入框获得焦点时的边框颜色和粗细 */
  outline: none; /* 去除输入框的默认轮廓样式 */
  box-shadow: 0 0 5px #dbdbdb; /* 设置输入框获得焦点时的阴影效果 */
}
</style>